package com.dshore.messagecenter.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**
 * openfile 电子合同
 * @author panyi-com
 *
 */
public class WordEditOpenFileUtil {
	
	public static String getBuild(String  tmpFile, Map<String, String> contentMap,String name) throws IOException {
        InputStream inputStream = WordToPdfUtil.class.getClassLoader().getResourceAsStream(tmpFile);
        HWPFDocument document = null;
        try {
            document = new HWPFDocument(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 读取文本内容
        Range bodyRange = document.getRange();
        // 替换内容
        for (Map.Entry<String, String> entry : contentMap.entrySet()) {
            bodyRange.replaceText("${" + entry.getKey() + "}", entry.getValue());
        }

        //电子合同临时。doc文件
        String fileName = name+".doc";
        //电子合同最终.pdf文件
        String pdfName = name+".pdf";
        try {
            String rootPath = System.getProperty("user.dir");
            File dstPath = new File(rootPath + "/wordtopdf");
            dstPath.mkdirs();
            //String fileNames = fileName;
            File f = new File(dstPath.getAbsolutePath() + "/" + fileName);

            FileOutputStream fos = new FileOutputStream(f);
            document.write(fos);
            fos.close();
            System.out.println(f.length());
            //修改好的word路径
            String start = dstPath.getAbsolutePath() + "/" +fileName;
            //最终返回的pdf路径
            String over = dstPath.getAbsolutePath() + "/" +pdfName;
            
            //调用方法把零时doc文件转为pdf文件
            File file = WordToPDF(start, over);
            //转换完成删除零时。doc文件
            f.delete();
           
            return over;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } 
	
	
	public static File WordToPDF(String startFile, String overFile) throws IOException {
        // 源文件目录
        File inputFile = new File(startFile);
        if (!inputFile.exists()) {
            System.out.println("源文件不存在！");
            return null;
        }
        
        // 如果目标路径不存在, 则新建该路径
        File outputFile = new File(overFile);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs(); 
        }

        // 调用openoffice服务线程
        String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4\\program\\soffice.exe";//这里是OpenOffice的安装目录, 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的  
        
        //启动OpenOffice的服务  
        String command = OpenOffice_HOME+ " -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";   
        System.out.println("command:"+command);
        Process pro = Runtime.getRuntime().exec(command); 
        
        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
        connection.connect();

        // 转换
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outputFile);

        // 关闭连接
        connection.disconnect();

        // 关闭进程
        pro.destroy();
        return outputFile;
    }

}
